﻿@using System.Security.Claims
@inject AuthenticationStateProvider AuthStateProvider
@inject ILocalStorageService LocalStorage


<AuthorizeView>
    <Authorized>
        <span class="ms-3">@context.User.Identity.Name</span>
        <a href="user/changepwd/@_id">修改密码</a>
        <a @onclick="Logout" href="javascript:void(0)">退出</a>
    </Authorized>
    <NotAuthorized>
        <a href="login">登录</a>
    </NotAuthorized>
</AuthorizeView>

@code {
    private string? _id;
    protected override async Task OnInitializedAsync()
    {
        //获取当前登录的用户
        _id =await GetUserIdAsync();
    }
    private async Task Logout()
    {
        await LocalStorage.RemoveItemAsync("token");
        _id = await GetUserIdAsync();
    }

    private async Task<string?> GetUserIdAsync()
    {
        var state = await AuthStateProvider.GetAuthenticationStateAsync();
        return state.User.FindFirst(x => x.Type.Contains("nameidentifier"))?.Value!;
    }
}
